/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package to.pm.acquisti.core;

import java.io.Serializable;
import javax.jms.Connection;
import java.util.logging.Logger;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;

/**
 *
 * @author fra
 */


public class NotificaOrdineEsterno {

    private  ConnectionFactory connectionFactory;
    private  Queue queue;


    private Serializable message;

    private Logger log = Logger.getAnonymousLogger();
    {
        log.info("Notifica creazione ordine esterno avvio...");
    }

  
    public void send()  {
        Connection connection = null;
        Destination dest = null;
        
        try {
            log.info("send:");

            log.info("queue " +queue.getQueueName());

            dest = (Destination) queue;


        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create producer and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
            connection = connectionFactory.createConnection();

            Session session = connection.createSession(
                        false,
                        Session.AUTO_ACKNOWLEDGE);

            MessageProducer producer = session.createProducer(dest);

            ObjectMessage msg = session.createObjectMessage();
            msg.setObject(getMessage());

           
            log.info("Sending message: " + msg.getObject());
            producer.send(msg);
      

        } catch (JMSException e) {
            throw (new RuntimeException(e));
        }  finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }

    public void setConnectionFactory(ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    public void setQueue(Queue queue) {
        this.queue = queue;
    }

    public void setMessage(Serializable message) {
        this.message = message;
    }

    public Serializable getMessage() {
        return message;
    }



}

